package com.example.auth.security;

import com.example.auth.utils.JwtUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JwtAuthenticationFilter extends BasicAuthenticationFilter {

    private static final String TOKEN = "token";

    public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String token = request.getHeader(TOKEN);
        //如果请求头中没有Authorization信息则直接放行了
        if (token == null || "".equals(token)) {
            chain.doFilter(request, response);
            return;
        }
        //如果请求头中有token,则进行解析，并且设置认证信息
        if (!JwtUtils.isExpiration(token)) {
            //设置上下文
            UsernamePasswordAuthenticationToken authenticationToken = getAuthentication(token);
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        super.doFilterInternal(request, response, chain);
    }

    //获取用户信息
    private UsernamePasswordAuthenticationToken getAuthentication(String token) {
        String username = JwtUtils.getUserName(token);
        // 获得权限 添加到权限上去
        String role = JwtUtils.getUserRole(token);
        List<GrantedAuthority> roles = new ArrayList<>();
        roles.add(new GrantedAuthority() {
                      @Override
                      public String getAuthority() {
                          return role;
                      }
                  });
        if (username != null) {
            return new UsernamePasswordAuthenticationToken(username, null, roles);
        }
        return null;
    }

}


